/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2020. All rights reserved.
 */

package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.device.idea.meeting.MeetingCallback;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.presenter.MeetingManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名：ExtendViewMeetingSettings
 * 描述：更多-设置里面的延长会议的view
 */
public class ExtendViewMeetingSettings extends BaseView implements View.OnClickListener {
    private static String TAG = ExtendViewMeetingSettings.class.getSimpleName();
    private ExtendTimePicker hourPicker;
    private ExtendTimePicker minutePicker;
    private int extendHour;
    private int extendMin;
    private TextView mid_extend_tv_cancel;
    private RelativeLayout extendConfTvForTry;
    private Runnable onDialogDismissEvent;

    public ExtendViewMeetingSettings(Context mContext) {
        super(mContext);
        init();
        initData();
    }

    private void init() {
        if (mRootView == null) {
            LogUtil.error(TAG + " init error, mRootView is null");
            return;
        }
        extendConfTvForTry = mRootView.findViewById(R.id.extend_conf_for_try);
        extendConfTvForTry.setVisibility(View.GONE);
        TextView extendTv = mRootView.findViewById(R.id.mid_extend_tv);
        extendTv.setOnClickListener(this);
        mid_extend_tv_cancel = mRootView.findViewById(R.id.mid_extend_tv_cancel);
        mid_extend_tv_cancel.setOnClickListener(this);
        hourPicker = mRootView.findViewById(R.id.mid_hour_pv);
        minutePicker = mRootView.findViewById(R.id.mid_minute_pv);
        extendHour = 1; // 默认 选中1 小时 + 30 分钟
        extendMin = 30;
    }

    private void initData() {
        List<String> data = new ArrayList<String>();
        List<String> seconds = new ArrayList<String>();
        for (int i = 0; i <= 6; i++) {
            data.add("" + i);
        }
        for (int i = 0; i <= 45; i = i + 15) {
            seconds.add(i < 10 ? "0" + i : "" + i);
        }
        hourPicker.setIsScroll(false); // 不循环滚动
        hourPicker.setData(data);
        hourPicker.setSelected(1); // 默认1小时
        hourPicker.setOnSelectListener(hourPickerSelectListener);
        minutePicker.setIsScroll(false); // 不循环滚动
        minutePicker.setData(seconds);
        minutePicker.setOnSelectListener(minutePickerSelectListener);
    }

    private ExtendTimePicker.OnSelectListener minutePickerSelectListener = new ExtendTimePicker.OnSelectListener() {
        @Override
        public void onSelect(String text) {
            // 取前两位，转化为分钟数
            try {
                extendMin = Integer.valueOf(text.substring(0, 2));
            } catch (NumberFormatException e) {
                LogUtil.info(TAG + " minutePicker OnSelect error, txt: " + text);
                extendMin = 0;
            }

            LogUtil.info(TAG + " extendMin: " + extendMin);
        }
    };

    private ExtendTimePicker.OnSelectListener hourPickerSelectListener =
            new ExtendTimePicker.OnSelectListener() {
                @Override
                public void onSelect(String text) {
                    // 取前两位，转化为小时数
                    try {
                        extendHour = Integer.valueOf(text.substring(0, 1));
                        LogUtil.info(TAG + " extendHour: " + extendHour);
                    } catch (NumberFormatException e) {
                        LogUtil.error(TAG + " hourPicker OnSelect error, txt: " + text);
                        extendHour = 0;
                    }

                    // 如果小时数选成6，那么分钟数就固定设置成0，不能滚动
                    if (extendHour == 6) {
                        List<String> minList = new ArrayList<String>();
                        minList.add("00");
                        minutePicker.setData(minList);

                        extendMin = 0;
                    } else if (extendHour == 0) { // 如果小时数选成0，那么分钟数不能选0
                        List<String> minList = new ArrayList<String>();
                        minList.add("15");
                        minList.add("30");
                        minList.add("45");
                        minutePicker.setData(minList);

                        // 此时默认选在30分钟
                        extendMin = 30;
                    } else {
                        List<String> newSeconds = new ArrayList<String>();
                        for (int i = 0; i <= 45; i = i + 15) {
                            newSeconds.add(i < 10 ? "0" + i : "" + i);
                        }
                        minutePicker.setData(newSeconds);

                        // 此时默认选在30分钟
                        extendMin = 30;
                    }
                }
            };

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.mid_extend_tv) {
            isNeedReqChair();
        }
        if (view.getId() == R.id.mid_extend_tv_cancel) {
            setDialogDismiss();
        }
    }

    private void setDialogDismiss() {
        if (onDialogDismissEvent != null) {
            onDialogDismissEvent.run();
        }
    }

    /**
     * 设置取消事件回调监听
     *
     * @param onDialogDismissEvent 事件回调监听
     */
    public void setOnDialogDismissEvent(Runnable onDialogDismissEvent) {
        this.onDialogDismissEvent = onDialogDismissEvent;
    }

    private void confExtendCmd() {
        LogUtil.info(TAG + " confExtendCmd");
        int leftTime = MeetingManager.getInstance().getConfLeftTime();
        LogUtil.info(TAG + " confExtendCmd leftTime:" + leftTime);
        if (leftTime < 0) {
            LogUtil.info(TAG + " A user try to extend the permanent conference.");
            LogUtil.info(TAG + " confExtendCmd error because conf is permanent conf");
            updateToast(R.string.extend_time_error_in_permenent_conf);
            return;
        }

        LogUtil.info(TAG + " extendHour: " + extendHour + ", extendMin: " + extendMin);
        int totalExtent = extendHour * 60 + extendMin;
        LogUtil.info(TAG + " A user try to extend the conference.");
        MeetingManager.getInstance().postponeConference(totalExtent, new MeetingCallback() {
            @Override
            public void onSuccess(Object succMsg) {
                LogUtil.debug(TAG + " postponeConference success");
                updateToast(R.string.extend_conf_success);
            }

            @Override
            public void onFailed(int errCode, String errMsg) {
                LogUtil.debug(TAG + " postponeConference failed");
                updateToast(R.string.extend_conf_failed);
            }
        });
    }

    private void isNeedReqChair() {
        if (MeetingManager.getInstance().isMeChairman()) {
            confExtendCmd();
        } else {
            updateToast(R.string.extend_conf_need_be_chairman);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.meeting_settings_extend_time;
    }

    @Override
    public void viewDataChanged(int eventId, Object obj) {
    }

    @Override
    public void unRegisterListenService() {
    }

    @Override
    public void registerListenerService() {
    }
}
